package demo_0428;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setBounds(100,100,400,300);
        JTextField textField1 = new JTextField();
        textField1.setColumns(20);

        JTextField textField2 = new JTextField();
        textField2.setColumns(20);

        JButton button = new JButton("提交");
        JTextField textField3 = new JTextField();
        textField3.setColumns(20);
        button.addActionListener(new ButtonListener(textField3) {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text1 = textField1.getText();
                String text2 = textField2.getText();
                Double num1 = Double.parseDouble(text1);
                Double num2 = Double.parseDouble(text2);
                Double num3 = num1 + num2;
                textField3.setText(num3.toString());
            }
        });

        frame.add(textField1);
        frame.add(textField2);
        frame.add(button);
        frame.add(textField3);



        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


class ButtonListener implements ActionListener {
    JTextField textField;
    public ButtonListener(JTextField textField){
        this.textField = textField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 在这里处理按钮点击事件，例如获取文本框中的内容并打印出来
        String text = textField.getText();
        Double num = Double.parseDouble(text);
    }
}